const app = getApp()

//问题类
class Question {

  /**
   * topic：题目
   * answer：答案
   * value：用户输入的值
   * isTrue:答案是否正确，默认是否
   */
  constructor(topic, answer, value, isTrue = false) {
    this.topic = topic;
    this.answer = answer;
    this.value = value;
    this.isTrue = isTrue;
  }
  /**
   * 根据难度获取运算符
   * rate:1/2/3
   * 返回+ - * /
   */
  getOperation(rate) {
    //如果是1/2那么只有加减
    let num = 0;
    if (rate == 1 || rate == 2) {
      num = Math.floor(Math.random() * 10) % 2;//返回0/1
    } else if (rate == 3) {
      //如果是3那么加减乘除
      num = Math.floor(Math.random() * 10) % 4;//返回0/1/2/3
    }
    if (0 == num) {
      return '+';
    } else if (1 == num) {
      return '-';
    } else if (2 == num) {
      return '*';
    } else if (3 == num) {
      return '/';
    }
  }

  /**
   * 根据数值和操作符计算结果
   * num1：数值1
   * num2：数值2
   * operation：运算符，+ - * /
   */
  getAnswer(num1, num2, operation) {
    let answer;
    if ('+' == operation) {
      answer = num1 + num2;
    } else if ('-' == operation) {
      answer = num1 - num2;
    } else if ('*' == operation) {
      answer = num1 * num2;
    } else if ('/' == operation) {
      answer = num1 / num2;
      //除法取小数点后两位
      answer = answer.toFixed(2);
    }
    return answer;
  }

  /**
   * 根据难度获取一个题目的方法
   * count:生成题目数
   * rate:难度等级，1/2/3
   */
  getQuestion(rate) {
    let num1;
    let num2;
    let operation;
    let answer;
    if (1 == rate) {
      num1 = Math.floor(Math.random() * 10);//不可能大于等于10的数值并向下取整
      num2 = Math.floor(Math.random() * 10);//不可能大于等于10的数值并向下取整
      operation = this.getOperation(rate);
      answer = this.getAnswer(num1, num2, operation);
    } else if (2 == rate) {
      num1 = Math.floor(Math.random() * 100);//不可能大于等于100的数值并向下取整
      num2 = Math.floor(Math.random() * 100);//不可能大于等于100的数值并向下取整
      operation = this.getOperation(rate);
      answer = this.getAnswer(num1, num2, operation);
    } else if (3 == rate) {
      num1 = Math.floor(Math.random() * 100);//不可能大于等于10的数值并向下取整
      num2 = Math.floor(Math.random() * 100);//不可能大于等于10的数值并向下取整
      operation = this.getOperation(rate);
      answer = this.getAnswer(num1, num2, operation);
    }
    //构建topic题目
    let tips = '';//默认提示是空
    if ('/' == operation) {
      //如果是除法，提示答案取小数点后两位
      tips = '(取小数点后两位)';
    }
    let topic = num1 + ' ' + operation + ' ' + num2 + ' = ?' + tips;
    return new Question(topic, answer);
  }

  /**
   * 根据数量和难度获取题目的方法
   * count:生成题目数
   * rate:难度等级，1/2/3
   * 返回题目数组
   */
  getQuestions(count, rate) {
    let questions = new Array();
    for (let i = 0; i < count; i++) {
      questions.push(this.getQuestion(rate));
    }
    return questions;
  }


}



Page({
  data: {
    index: 0
  },
  onLoad: function (options) {
    let count = options.count;
    let rate = options.rate;
    let q = new Question();
    let questions = q.getQuestions(count, rate);
    wx.setStorageSync('questions', questions);
    this.setData({
      questions: questions
    })
  },
  answerInput(e) {
    let questions = this.data.questions
    let index = this.data.index
    // questions[index].value = parseInt(e.detail.value)
    questions[index].value = e.detail.value
    if (questions[index].answer == questions[index].value) {
      questions[index].isTrue = true;
    }else{
      questions[index].isTrue = false;
    }
    this.setData({
      questions: questions
    })
  },
  last() {
    this.setData({
      index: this.data.index - 1
    })
  },
  next() {
    this.setData({
      index: this.data.index + 1
    })
  },
  formSubmit: function (e) {
    wx.setStorageSync('questions', this.data.questions);
    wx.redirectTo({
      url: '/pages/answer/answer',
    })
  }
})